<template>
	<view class="editings">
		<view class="editings-son">
			<!-- <view v-if="!user.userName">
				<fui-input label="账号" textAlign="right" borderTop placeholder="待完善"></fui-input>
			</view>
			<view v-if="user.userName">
				<fui-input label="账号" readonly @click="" v-model="user.userName" textAlign="right"
					borderTop></fui-input>
			</view> -->
			<view v-if="!user.nickName">
				<fui-input label="姓名" textAlign="right" @click="onSetUserNmae" borderTop placeholder="待完善"></fui-input>
			</view>
			
			<view v-if="user.nickName"  >
				<fui-input label="姓名" readonly @click="onSetUserNmae" v-model="user.nickName" textAlign="right"
					borderTop></fui-input>
			</view>
			
			

			<view v-if="!user.birthday">
				<fui-input label="生日" @click="showTime = !showTime" readonly textAlign="right" borderTop
					placeholder="待完善"></fui-input>
			</view>

			<view v-if="user.birthday">
				<fui-input label="生日" @click="showTime = !showTime" v-model="user.birthday" readonly textAlign="right" borderTop></fui-input>
			</view>

			<view v-if="!user.sex" @click="showSex = !showSex">
				<fui-input label="性别" readonly textAlign="right" borderTop placeholder="待完善"></fui-input>
			</view>

			<view v-if="user.sex">
				<fui-input  @click="showSex = !showSex"  label="性别" v-model="user.sex" readonly textAlign="right" borderTop></fui-input>
			</view>
			<br />
			
			<view v-if="!user.resume">
				<fui-input label="简介" readonly @click="onShowResume" textAlign="right" borderTop
					placeholder="待完善"></fui-input>
			</view>
			
			<view v-if="user.resume" style="width: 90%; margin: 0 auto;">
				<up-text @click="onShowResume" :size="18" text="简介"></up-text>
				<up-textarea border="bottom" @click="onShowResume"   disabled maxlength="25" borderTop  v-model="user.resume"></up-textarea>
			</view>
		</view>

		<view>
			<u-action-sheet :show="showSex" :actions="actions" title="请选择性别" description="如果选择保密会报错"
				@close="showSex = false" @select="sexSelect">
			</u-action-sheet>
		</view>
		<view>
			<u-modal showCancelButton @confirm="setUserNmae" @cancel="closeNewUserNmae" :show="showUserNmae"
				title="请输入姓名">
				<view class="slot-content">
					<up-input placeholder="请输入新姓名" maxlength="15" border="surround" v-model="newUserName"></up-input>
				</view>
			</u-modal>
		</view>
		<view>
			<u-modal showCancelButton @confirm="setResume" @cancel="closeResume" :show="showResume" title="请输入简介">
				<view class="slot-content">
					<up-input placeholder="在这里输入简介" maxlength="25" border="bottom" v-model="resume"></up-input>
				</view>
			</u-modal>
		</view>
		<view>
			<u-datetime-picker :show="showTime" @cancel="closeTime" @confirm="onTime" v-model="newTime"
				:minDate="minDate" :maxDate="maxDate" mode="date"></u-datetime-picker>
		</view>
	</view>
</template>

<script>
	import {BASE_IP} from '@/common/http/url.js'
	export default {
		name: "editing",
		data() {
			return {
				showSex: false,
				model1: {
					userInfo: {
						name: 'uview-plus UI',
						sex: '',
					},
				},
				actions: [{
						name: '男',
					},
					{
						name: '女',
					},
					{
						name: '保密',
					},
				],
				rules: {
					'userInfo.name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					'userInfo.sex': {
						type: 'string',
						max: 1,
						required: true,
						message: '请选择男或女',
						trigger: ['blur', 'change']
					},
				},
				radio: '',
				switchVal: false,
				user: "",
				showUserNmae: false,
				newUserName: "",
				showResume: false,
				resume: "",
				newSex: "",
				showTime: false,
				newTime: "",
				minDate: 0, // 当前日期的时间戳，将时间部分设置为 0
				maxDate: new Date().setHours(23, 59, 59, 999), // 设置最大日期，根据您的需求自行修改
			}
		},
		methods: {
			onSetUserNmae() {
				this.showUserNmae = true
			},
			setUserNmae() {
				if (this.newUserName == "") {
					uni.showToast({
						title: "姓名未输入",
						icon: "error"
					})
					// @PostMapping("/pc/user/update/userName/{userName}/{name}")
					return
				}
				let url = BASE_IP + "pc/user/update/nickName/" + this.user.userName + "/" + this.newUserName
				// console.log("获取链接：",url)
				let _this = this
				uni.request({
					header: {
						"Authorization": this.user.token
					},
					url:url,
					method:"POST",
					success: (res) => {
						console.log(res)
						if(res.statusCode == 200){
							_this.user.nickName = _this.newUserName
							uni.setStorageSync("user",_this.user)
							uni.showToast({
								title:"更新成功",
								icon:"success"
							})
							uni.navigateTo({
								url:"/pages/index/index",
								success() {
									uni.navigateBack()
								}
							})
							return
						}
						if(res.statusCode == 404){
							uni.showToast({
								title:"失败",
								icon:"error"
							})
							return
						}
						if(res.statusCode == 500){
							uni.showToast({
								title:"失败",
								icon:"error"
							})
							return
						}
					}
				})
				this.showUserNmae = false
			},
			closeNewUserNmae() {
				this.showUserNmae = false
			},
			onShowResume() {
				this.showResume = true
			},
			closeResume() {
				this.showResume = false
			},
			setResume() {
				if (this.resume == "") {
					uni.showToast({
						title: "请输入新的简介",
						icon: "error"
					})
					return
				}
				let url = BASE_IP + "pc/user/update/resume/" + this.user.userName + "/" + this.resume
				uni.request({
					header: {
						"Authorization": this.user.token
					},
					url:url,
					method:"POST",
					success: (res) => {
						console.log(res)
						if(res.statusCode == 200){
							uni.showToast({
								title:"修改成功",
								icon:"success"
							})
							this.user.resume = this.resume
							uni.setStorageSync("user",this.user)
							uni.showToast({
								title:"更新成功",
								icon:"success"
							})
							uni.navigateTo({
								url:"/pages/index/index",
								success() {
									uni.navigateBack()
								}
							})
							return
						}
						if(res.statusCode == 500){
							uni.showToast({
								title:"修改失败",
								icon:"error"
							})
							return
						}
						
						if(res.statusCode == 404){
							uni.showToast({
								title:"修改失败",
								icon:"error"
							})
							return
						}
					}
				})
				
				this.showResume = false
			},
			sexSelect(res) {
				this.newSex = res.name
				let url = BASE_IP + "pc/user/update/sex/" + this.user.userName + "/" + this.newSex
				uni.request({
					header: {
						"Authorization": this.user.token
					},
					url:url,
					method:"POST",
					success: (res) => {
						console.log(res)
						if(res.statusCode == 200){
							uni.showToast({
								title:"修改成功",
								icon:"success"
							})
							this.user.sex = this.newSex
							uni.setStorageSync("user",this.user)
							uni.showToast({
								title:"更新成功",
								icon:"success"
							})
							uni.navigateTo({
								url:"/pages/index/index",
								success() {
									uni.navigateBack()
								}
							})
							return
						}
						if(res.statusCode == 500){
							uni.showToast({
								title:"修改失败",
								icon:"error"
							})
							return
						}
						
						if(res.statusCode == 404){
							uni.showToast({
								title:"修改失败",
								icon:"error"
							})
							return
						}
					}
				})
			},
			onTime(res) {
				let timestamp = res.value; // 时间戳
				let date = new Date(timestamp);
				let year = date.getFullYear();
				let month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份从 0 开始，所以需要加 1
				let day = date.getDate().toString().padStart(2, '0');
				let formattedDate = `${year}-${month}-${day}`;
				this.newTime = formattedDate;
				let url = BASE_IP + "pc/user/update/birthday/" + this.user.userName + "/" + this.newTime
				// console.log(url)
				// this.user.birthday = formattedDate
				// uni.setStorageSync("user",this.user)
				uni.request({
					header: {
						"Authorization": this.user.token
					},
					url:url,
					method:"POST",
					success: (res) => {
						console.log(res)
						if(res.statusCode == 200){
							uni.showToast({
								title:"修改成功",
								icon:"success"
							})
							this.user.birthday = formattedDate
							uni.setStorageSync("user",this.user)
							uni.showToast({
								title:"更新成功",
								icon:"success"
							})
							// uni.navigateTo({
							// 	url:"/pages/index/index",
							// 	success() {
							// 		uni.navigateBack()
							// 	}
							// })
							return
						}
						if(res.statusCode == 500){
							uni.showToast({
								title:"修改失败",
								icon:"error"
							})
							return
						}
						
						if(res.statusCode == 404){
							uni.showToast({
								title:"修改失败",
								icon:"error"
							})
							return
						}
					}
				})
				this.showTime = false
			},
			
			closeTime() {
				this.showTime = false
			}

		},
		onLoad() {
			let user = uni.getStorageSync("user")
			this.user = user
			console.log(user)
		}
	}
</script>

<style>
	.editings {
		width: 100%;
		height: auto;
	}

	.editings-son {
		width: 95%;
		height: auto;
		margin: auto;
	}
</style>